﻿using Infrastructures;
using IRepository;
using Models;
using Spring.Data.Common;
using Spring.Data.Generic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryMySQL
{
    public class CompanyRepository : ICompanyRepository
    {

        private string baseCompany = "select CompanyNO,Name,ChargeName,TypeNO,JobTypeNO,Sign,SizeNO,CompanyHref,Address,LogoPath,CompanyLDNO,Point,Grade,CreateTime,UpdateTime,Status,Remark from Company";
        public Company Create(Company obj)
        {
            string strIns = @"insert into Company(CompanyNO,Name,ChargeName,TypeNO,JobTypeNO,Sign,SizeNO,CompanyHref,Address,LogoPath,CompanyLDNO,Point,Grade,CreateTime,UpdateTime,Status,Remark) values(@CompanyNO,@Name,@ChargeName,@TypeNO,@JobTypeNO,@Sign,@SizeNO,@CompanyHref,@Address,@LogoPath,@CompanyLDNO,@Point,@Grade,@CreateTime,@UpdateTime,@Status,@Remark);";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();

            p.Add("CompanyNO", DbType.Int32).Value = obj.CompanyNO;
            p.Add("Name", DbType.String).Value = obj.Name;
            p.Add("ChargeName", DbType.String).Value = obj.ChargeName;
            p.Add("TypeNO", DbType.Int32).Value = obj.TypeNO;
            p.Add("JobTypeNO", DbType.Int32).Value = obj.JobTypeNO;
            p.Add("Sign", DbType.String).Value = obj.Sign;
            p.Add("SizeNO", DbType.Int32).Value = obj.SizeNO;
            p.Add("CompanyHref", DbType.String).Value = obj.CompanyHref;
            p.Add("Address", DbType.String).Value = obj.Address;
            p.Add("LogoPath", DbType.String).Value = obj.LogoPath;
            p.Add("CompanyLDNO", DbType.String).Value = obj.CompanyLDNO;
            p.Add("Point", DbType.Double).Value = obj.Point;
            p.Add("Grade", DbType.String).Value = obj.Grade;
            p.Add("CreateTime", DbType.DateTime).Value = obj.CreateTime;
            p.Add("UpdateTime", DbType.DateTime).Value = obj.UpdateTime;
            p.Add("Status", DbType.String).Value = obj.Status;
            p.Add("Remark", DbType.String).Value = obj.Remark;

            DBHelper.TemplateMySQL.ExecuteNonQuery(CommandType.Text, strIns, p);
            return obj;
        }
        public void Update(Company obj)
        {
            string strUpdate = @"update Company set Name=@Name,ChargeName=@ChargeName,TypeNO=@TypeNO,JobTypeNO=@JobTypeNO,Sign=@Sign,SizeNO=@SizeNO,CompanyHref=@CompanyHref,Address=@Address,LogoPath=@LogoPath,CompanyLDNO=@CompanyLDNO,Point=@Point,Grade=@Grade,CreateTime=@CreateTime,UpdateTime=@UpdateTime,Status=@Status,Remark=@Remark where CompanyNO=@CompanyNO";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();

            p.Add("CompanyNO", DbType.Int32).Value = obj.CompanyNO;
            p.Add("Name", DbType.String).Value = obj.Name;
            p.Add("ChargeName", DbType.String).Value = obj.ChargeName;
            p.Add("TypeNO", DbType.Int32).Value = obj.TypeNO;
            p.Add("JobTypeNO", DbType.Int32).Value = obj.JobTypeNO;
            p.Add("Sign", DbType.String).Value = obj.Sign;
            p.Add("SizeNO", DbType.Int32).Value = obj.SizeNO;
            p.Add("CompanyHref", DbType.String).Value = obj.CompanyHref;
            p.Add("Address", DbType.String).Value = obj.Address;
            p.Add("LogoPath", DbType.String).Value = obj.LogoPath;
            p.Add("CompanyLDNO", DbType.String).Value = obj.CompanyLDNO;
            p.Add("Point", DbType.Double).Value = obj.Point;
            p.Add("Grade", DbType.String).Value = obj.Grade;
            p.Add("CreateTime", DbType.DateTime).Value = obj.CreateTime;
            p.Add("UpdateTime", DbType.DateTime).Value = obj.UpdateTime;
            p.Add("Status", DbType.String).Value = obj.Status;
            p.Add("Remark", DbType.String).Value = obj.Remark;

            DBHelper.TemplateMySQL.ExecuteNonQuery(CommandType.Text, strUpdate, p);
        }
        public Company Get(string companyno)
        {
            string strSel = baseCompany + " where CompanyNO=@CompanyNO";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();
            p.Add("CompanyNO", DbType.Int32).Value = companyno.ToInt();

            IList<Company> lst = DBHelper.TemplateMySQL.QueryWithRowMapperDelegate(CommandType.Text, strSel, new RowMapperDelegate<Company>(MapCompany), p);
            return lst.Count == 0 ? null : lst[0];
        }
        public IList<Company> List()
        {
            string strSel = baseCompany;

            IList<Company> lst = DBHelper.TemplateMySQL.QueryWithRowMapperDelegate(CommandType.Text, strSel, new RowMapperDelegate<Company>(MapCompany));

            return lst;
        }

        private Company MapCompany(IDataReader dr, int count)
        {
            Company obj = new Company();
            obj.CompanyNO = dr.GetInt32(0);
            obj.Name = dr.IsDBNull(1) ? String.Empty : dr.GetString(1);
            obj.ChargeName = dr.IsDBNull(2) ? String.Empty : dr.GetString(2);
            obj.TypeNO = dr.GetInt32(3);
            obj.JobTypeNO = dr.GetInt32(4);
            obj.Sign = dr.IsDBNull(5) ? String.Empty : dr.GetString(5);
            obj.SizeNO = dr.GetInt32(6);
            obj.CompanyHref = dr.IsDBNull(7) ? String.Empty : dr.GetString(7);
            obj.Address = dr.IsDBNull(8) ? String.Empty : dr.GetString(8);
            obj.LogoPath = dr.IsDBNull(9) ? String.Empty : dr.GetString(9);
            obj.CompanyLDNO = dr.IsDBNull(10) ? String.Empty : dr.GetString(10);
            obj.Point = dr.GetDouble(11);
            obj.Grade = dr.IsDBNull(12) ? String.Empty : dr.GetString(12);
            obj.CreateTime = dr.GetDateTime(13);
            obj.UpdateTime = dr.GetDateTime(14);
            obj.Status = (CommonStatus)Enum.Parse(typeof(CommonStatus), dr.GetString(15), true);
            obj.Remark = dr.IsDBNull(16) ? String.Empty : dr.GetString(16);

            return obj;
        }
    }
}

